<?php
header('Content-Type: text/html; charset=utf-8');
require ('./common/func.php');
list($sendEmail,$sendEmailPwd,$sendName,$sendTitle, $content,$toEmail, $template, $host, $port) = func_checkParams();
require ('./mail/db.php');
$sql = "SELECT * FROM `template` where k='{$template}' LIMIT 1";
$res = Database::query($sql);
if (empty($res) && in_array($template,[1,2,3,4])) {
    require("./template/{$template}.php");
} else if(!empty($res)){
    $content = $res[0]['v'];
    $templateParams = [
        "template_img" => "[头像_URL]",
        "template_qq" => "[QQ号码]",
        "template_nick" => "[对方昵称]",
        "template_randNum" => "[随机数]",
        "template_randStr" => "[随机字符]",
        "template_randText" => "[随机一句]",
        "template_url" => "[加群_URL]",
        "template_time" => "[当前时间]"
    ];

    $template_url = func_request('url');
    $template_url = urlencode($template_url);
    $template_qq = func_request('qq');
    $template_time = date('Y/m/d H:i:s');
    $template_img = func_getQQLogo($template_qq);
    $template_nick = func_getQQNick($template_qq);
    $template_randStr = func_randStr();
    $template_randNum = func_randNum();


    if(strlen($template_url) < 5) {
        func_response('跳转地址不符合');
    }
    if (strlen($template_qq) < 5) {
        func_response('QQ号不符合');
    }
    $template_randText = func_randText();
    foreach ($templateParams as $k => $item) {
        if(strpos($content,$item) !== false) {
            $content = str_replace($item, $$k, $content);
        }
    }

}
if($content) {
    $params = [$sendEmail,$sendEmailPwd,$sendName,$sendTitle, $content,$toEmail, $host, $port];
    $resualt = func_sendEmail($params);
    if ($resualt === true) {
        func_response('success');
    }
    func_response($resualt);
} else {
    func_response("没有相应的模板error");
}
exit;
